package Dao;

import Model.User;

import java.sql.*;

public class UserDao {
    public static Connection getConnection() {
        Connection conn = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/bill", "root", "123456");
            return conn;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return conn;
    }

    public static void release(PreparedStatement pstmt, Connection conn) {
        release(null,pstmt, conn);
    }

    public static void release(ResultSet rs, PreparedStatement pstmt, Connection conn) {
        try {
            if (rs != null) {
                rs.close();
            }
            if (pstmt != null) {
                pstmt.close();
            }
            if (conn != null) {
                conn.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public static void setUser() {
        User o1 = new User(4001,"123");
        UserDao.insert(o1);
    }
    public static void deleteAll() {
        try {
            String sql = "delete from user ";
            Connection conn = getConnection();
            PreparedStatement stat = conn.prepareStatement(sql);
            stat.executeUpdate();
            release(stat, conn);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public static void insert(User model){
        try {
            String sql = "insert into user(id,password) values(?,?);";
            Connection conn = getConnection();
            PreparedStatement stat = conn.prepareStatement(sql);
            stat.setInt(1, model.getId());
            stat.setString(2, model.getPassword());
            stat.executeUpdate();
            release(stat, conn);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public static boolean selectExist(User model) {
        ResultSet rs = null;
        boolean flag=false;
        PreparedStatement stat=null;
        Connection conn = getConnection();
        try {
            String sql = "select * from user where id=? and password=?";
            stat = conn.prepareStatement(sql);
            stat.setInt(1, model.getId());
            stat.setString(2,model.getPassword());
            rs = stat.executeQuery();
            if(rs.next()){
                flag=true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            release(rs,stat,conn);
        }
        return flag;
    }
    public static Integer selectNewId() {
        ResultSet rs = null;
        PreparedStatement stat = null;
        Connection conn = getConnection();
        int a = 1000; // 默认ID值
        try {
            String sql = "select MAX(id) from user";
            stat = conn.prepareStatement(sql);
            rs = stat.executeQuery();
            if (rs.next()) {
                int maxId = rs.getInt(1);
                a = maxId + 1;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            release(rs, stat, conn);
        }
        return a;
    }
    public static void insertUserNew(User model) {
        try {
            String sql = "insert into user(id,password) values(?,?);";
            Connection conn = getConnection();
            PreparedStatement stat = conn.prepareStatement(sql);
            stat.setInt(1, model.getId());
            stat.setString(2, model.getPassword());
            stat.executeUpdate();
            release(stat, conn);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public static boolean updateUserKey(User model) {
        boolean flag=false;
        if (model == null) {
            return flag;
        }
        try {
            String sql = "update user set password=? where id=?";
            Connection conn = getConnection();
            PreparedStatement stat = conn.prepareStatement(sql);
            stat.setString(1, model.getPassword());
            stat.setInt(2,model.getId());
            stat.executeUpdate();
            release(stat, conn);
            flag=true;
            return flag;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return flag;
    }
}
